package com.yxhl.zoume.account.presenter;

import android.content.Context;

import com.yxhl.zoume.R;
import com.yxhl.zoume.account.view.MobileLoginView;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.presenter.IPresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.data.http.rest.param.login.GetSmsCodeParam;
import com.yxhl.zoume.data.http.rest.param.login.MobileLoginParam;
import com.yxhl.zoume.data.http.rest.response.login.GetSmsCodeResponse;
import com.yxhl.zoume.data.http.rest.response.login.MobileLoginResponse;
import com.yxhl.zoume.di.scope.PerActivity;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.user.GetSmsCodeUseCase;
import com.yxhl.zoume.domain.interactor.user.MobileLoginUseCase;
import com.yxhl.zoume.utils.NetworkUtils;
import com.yxhl.zoume.utils.PromptUtils;

import javax.inject.Inject;

/**
 * Created by Xboy on 16/3/20.
 */

@PerActivity
public class MobileLoginPresenter extends BasePresenter implements IPresenter {
    private final GetSmsCodeUseCase getSmsCodeUseCase;
    private final MobileLoginUseCase mobileLoginUseCase;
    private MobileLoginView mobileLoginView;

    @Inject
    public MobileLoginPresenter(Context context,
                                GetSmsCodeUseCase getSmsCodeUseCase,
                                MobileLoginUseCase mobileLoginUseCase) {
        super(context);
        this.getSmsCodeUseCase = getSmsCodeUseCase;
        this.mobileLoginUseCase = mobileLoginUseCase;
    }

    @Override
    public void attachView(BaseView view) {
        mobileLoginView = (MobileLoginView) view;
    }

    /**
     * 获取短信验证码
     *
     * @param mobile
     */
    public void fetchSmsCode(String mobile) {
        // show loading
        mobileLoginView.showLoadingIndicator();

        // request
        getSmsCodeUseCase.execute(new GetSmsCodeParam(mobile)
                , new ZMSubscriber<GetSmsCodeResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        System.out.println("login-------> "+e);
                        mobileLoginView.hideLoadingIndicator();
                    }

                    @Override
                    public void onNext(GetSmsCodeResponse getSmsCodeResponse) {
                        // hide loading
                        mobileLoginView.hideLoadingIndicator();

                        if (getSmsCodeResponse.isSucc()) {
                            // show toast
                            mobileLoginView.showSmsCodeSendToast();
                        } else {
                            String resultCode = getSmsCodeResponse.getResultCode();
                            String resultMessage = getSmsCodeResponse.getResultMessage();
                            mobileLoginView.showSmsCodeErrorMessage(resultCode, resultMessage);
                        }
                    }
                });
    }

    /**
     * 手机号登录
     *
     * @param mobile
     * @param smsCode
     */
    public void loginByMobile(final String mobile, String smsCode, String deviceToken) {
        // show loading
        mobileLoginView.showLoadingIndicator();

        MobileLoginParam param = new MobileLoginParam(mobile, smsCode, deviceToken);
        param.setClientSystem(CLIENT_SYSTEM);
        param.setClientVersion(getClientVersionName());
        mobileLoginUseCase.execute(param, new ZMSubscriber<MobileLoginResponse>() {
            @Override
            public void onCompleted() {
                // hide loading
                mobileLoginView.hideLoadingIndicator();
            }

            @Override
            public void onError(Throwable e) {
                // hide loading
                mobileLoginView.hideLoadingIndicator();
            }

            @Override
            public void onNext(MobileLoginResponse mobileLoginResponse) {

                boolean succ = mobileLoginResponse.isSucc();
                if (succ) {
                    mobileLoginView.renderMobileLoginResult(mobileLoginResponse);
                } else {
                    String resultCode = mobileLoginResponse.getResultCode();
                    String resultMessage = mobileLoginResponse.getResultMessage();
                    mobileLoginView.showErrorMessage(resultCode, resultMessage);
                }
            }
        });
    }

    @Override
    public void onResume() {
        if (!NetworkUtils.getInstance(mContext).isConnected()) {
            PromptUtils.showShort(mContext, mContext.getString(R.string.network_not_available));
        }
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestroy() {
        getSmsCodeUseCase.unsubscribe();
        mobileLoginUseCase.unsubscribe();
    }
}
